package com.demo.serialize;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class AesUtil {

    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";

    /**
     *  加密  key 需 16位
     * @param content
     * @param key
     * @return
     */
    public static String encrypt(String content, String key) {
        try {
            //获得密码的字节数组
            byte[] raw = key.getBytes();
            //根据密码生成AES密钥
            SecretKeySpec skey = new SecretKeySpec(raw, "AES");
            //根据指定算法ALGORITHM自成密码器
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            //初始化密码器，第一个参数为加密(ENCRYPT_MODE)或者解密(DECRYPT_MODE)操作，第二个参数为生成的AES密钥 16位
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            //获取加密内容的字节数组(设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
            byte [] byte_content = content.getBytes("utf-8");
            //密码器加密数据
            byte [] encode_content = cipher.doFinal(byte_content);
            //将加密后的数据转换为Base64编码的字符串返回
            return Base64.encodeBase64String(encode_content);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     *  加密  key 需 16位
     * @param content
     * @param key
     * @return
     */
    public static byte[] encrypt(byte[] content, String key) {
        try {
            //获得密码的字节数组
            byte[] raw = key.getBytes();
            //根据密码生成AES密钥
            SecretKeySpec skey = new SecretKeySpec(raw, "AES");
            //根据指定算法ALGORITHM自成密码器
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            //初始化密码器，第一个参数为加密(ENCRYPT_MODE)或者解密(DECRYPT_MODE)操作，第二个参数为生成的AES密钥 16位
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            //密码器加密数据
            return cipher.doFinal(content);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 解密  key 需 16位
     * @param encrypt
     * @param decryptKey
     * @return
     */
    public static byte[] decrypt(byte[] encrypt, String decryptKey) {
        try {
            //获得密码的字节数组
            byte[] raw = decryptKey.getBytes();
            //根据密码生成AES密钥
            SecretKeySpec skey = new SecretKeySpec(raw, "AES");
            //根据指定算法ALGORITHM自成密码器
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            //初始化密码器，第一个参数为加密(ENCRYPT_MODE)或者解密(DECRYPT_MODE)操作，第二个参数为生成的AES密钥
            cipher.init(Cipher.DECRYPT_MODE, skey);
            //密码器解密数据
            return cipher.doFinal(encrypt);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 解密  key 需 16位
     * @param encryptStr
     * @param decryptKey
     * @return
     */
    public static String decrypt(String encryptStr, String decryptKey) {
        try {
            //获得密码的字节数组
            byte[] raw = decryptKey.getBytes();
            //根据密码生成AES密钥
            SecretKeySpec skey = new SecretKeySpec(raw, "AES");
            //根据指定算法ALGORITHM自成密码器
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            //初始化密码器，第一个参数为加密(ENCRYPT_MODE)或者解密(DECRYPT_MODE)操作，第二个参数为生成的AES密钥
            cipher.init(Cipher.DECRYPT_MODE, skey);
            //把密文字符串Base64转回密文字节数组
            byte [] encode_content = Base64.decodeBase64(encryptStr);
            //密码器解密数据
            byte [] byte_content = cipher.doFinal(encode_content);
            //将解密后的数据转换为字符串返回
            return new String(byte_content,"utf-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }





    private final static String KEY = "779381333@qq.com";

    public static void main(String[] args) {
        byte[] bytes = "廖凡庆123".getBytes(StandardCharsets.UTF_8);

        byte[] bytes1 = ByteCopyUtil.ByteSubstr(bytes, 0, bytes.length / 2);

        byte[] bytes2 = ByteCopyUtil.ByteSubstr(bytes, bytes.length / 2, bytes.length / 2);

        byte[] encrypt1 = encrypt(bytes1, KEY);
        byte[] encrypt2 = encrypt(bytes2, KEY);
        byte[] encrypt = ByteCopyUtil.ByteMerge(encrypt1,encrypt2);
        print(encrypt);

//        String str = encrypt("廖凡庆123", KEY);
//        System.out.println(str);
//
//        String str2 = decrypt(str, KEY);
//        System.out.println(str2);
    }

    private static void print(byte[] bytes){
        for (byte b : bytes) {
            System.out.print(b + " ");
        }
        System.out.println();
    }


}
